package com.example.web;

import com.example.strategy.strategy.factory.StrategyFactory;
import com.example.strategy.strategy.service.Strategy;
import com.example.strategy.strategy.service.impl.BusiAcctStrategy;
import com.example.util.ApplicationContextUtil;
import com.example.util.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    StrategyFactory strategyFactory;

    @Autowired
    ApplicationContextUtil applicationContextUtil;

    @GetMapping("/astest")
    public String hello(){
        System.out.println("boot");
        return "hello spring boot 你好";
    }

    @GetMapping("/{type}")
    public Double test(@PathVariable("type") Integer type){
        Strategy creator = strategyFactory.creator(type);
        if (creator!=null){
            System.out.println("creator>>>>>>>"+creator);
            Double recharge = creator.calRecharge(100D);
            System.out.println("recharge>>>>>>>"+recharge);
            return recharge;
        }
        return null;
    }

    @GetMapping
    public Double test2(){
        Strategy strategy=SpringUtils.getBean(BusiAcctStrategy.class);
        System.out.println("calRecharge>>>>>>>"+strategy.calRecharge(100D));

        Strategy strategy2=(Strategy)applicationContextUtil.getBean("cardStrategy");
        System.out.println("calRecharge>>>>>>>"+strategy2.calRecharge(100D));

        Strategy strategy3=SpringUtils.getBean("EBankStrategy");
        System.out.println("calRecharge>>>>>>>"+strategy3.calRecharge(100D));

        Strategy strategy4=SpringUtils.getBean("mobileStrategy");
        System.out.println("calRecharge>>>>>>>"+strategy4.calRecharge(100D));

        return 100D;
    }
}
